package com.expert.controller;
import com.expert.common.jqgrid.GridResult;
import com.expert.common.page.MyPage;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.expert.common.utils.FormUtils;
import com.expert.layui.LayuiDataGridResult;
import com.expert.layui.LayuiResult;
import org.springframework.stereotype.Controller;
import com.expert.common.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.expert.service.FeatureService;
import com.expert.entity.Feature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *   @description : Feature控制器
 *   ---------------------------------
 *   @author Mankind
 *   @since 2018-05-31
 */
@Controller
@RequestMapping("/console/feature")
public class FeatureController extends BaseController {

    @Autowired
    private FeatureService featureService;

    @ModelAttribute("feature")
    public Feature get(@RequestParam(required = false)Long id) throws Exception{
        if(id != null && id > 0){
            return featureService.selectById(id);
        }else{
            return new Feature();
        }
    }

    @RequestMapping(value="/list",produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        return "/admin/feature/list";
    }

    @RequestMapping(value="/list.json",produces = "application/json;charset=utf-8")
    @ResponseBody
    public GridResult list() throws Exception{
        MyPage<Feature> page = getPage();
        page.setAsc(false);
        page=featureService.list(page);
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    /**
     * 跳转添加/编辑界面
     * @param feature
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = {"/add", "/edit"},produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String add(@ModelAttribute("feature") Feature feature,Model model) throws Exception{
        model.addAttribute("feature",feature);
        return "/admin/feature/edit";
    }

    /**
     * 处理更新的请求
     * @param feature
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value={"/add", "/edit"},produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object>  form(@ModelAttribute("feature") Feature feature,Model model) throws Exception{
        Map<String,Object> result = new HashedMap();
        try{
            if(feature.getId()!=null&&feature.getId()>0){
                featureService.updateById(feature);
            }else{
                featureService.insert(feature);
            };
            result.put("status",200);
            result.put("msg","保存成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","保存失败");

        }

        return result;
    }

    /**
     * 处理删除的请求
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public  Map<String,Object> delete(String id) throws Exception{
        Map<String,Object> result = new HashedMap();
        if(StringUtils.isNotEmpty(id)){
            List<Long> idsArray = Arrays.asList(FormUtils.getIdsArray(id));
            try{
                result.put("status",200);
                result.put("msg","删除成功");
                featureService.deleteBatchIds(idsArray);
            }catch(Exception e){
                e.printStackTrace();
                result.put("status",500);
                result.put("msg","删除失败");
            }
        }
        return result;
    }

}